<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                                 |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//
namespace AuctionBack\Logic;

class ClcwMessageLogic extends BaseLogic
{
    protected $autoCheckFields = false;

    protected $clcwMessageModel;

    public function _initialize()
    {
        $this->clcwMessageModel = D('ClcwMessage', 'Model');
    }

    /**
     * 获取消息列表
     *
     * @param type $page
     * @param type $size
     * @param type $params
     *
     * @return array
     */
    public function getClcwMessageList($page, $size, $params)
    {
        $page = (int)$page < 1 ? 1 : (int)$page;
        $size = (int)$size < 1 ? 20 : (int)$size;
        $map = [];
        $map['sender'] = ['eq', 2];
        if (!empty($params['title'])) {
            $map['title'] = ['like', "%" . $params['title'] . "%"];
        }
        $cols = "id,title,content,receive,status,create_time,publish_time";
        $total = $this->clcwMessageModel->field($cols)
            ->where($map)
            ->count();
        $list = $this->clcwMessageModel->field($cols)
            ->where($map)
            ->limit($size)
            ->page($page)
            ->order('id DESC')
            ->select();
        $empId = [];

        array_map('strip_tags', $list);
        $result = [
            'list'  => $list,
            'total' => $total,
        ];
        return $result;
    }

    /**
     * 获取消息详情
     *
     * @param int $id
     *
     * @return array
     */
    public function getClcwMessage($id)
    {
        if (empty($id)) {
            return false;
        }
        return $this->clcwMessageModel->where(['id' => $id])->find();
    }

    /**
     * 添加消息数据
     *
     * @param array $data
     */
    public function addClcwMessage($data = [])
    {
        if (empty($data)) {
            return false;
        }
        $data['create_time'] = NOW_TIME;
        $receiveIds = $data['receive_ids'];
        unset($data['receive_ids']);
        $msgId = $this->clcwMessageModel->add($data);
        if (!empty($receiveIds)) {
            $receiveArr = explode(',', $receiveIds);
            foreach ($receiveArr as $k => $v) {
                if (!empty($v)) {
                    D('ClcwMessageReceive', 'Logic')->addMessageReceive(
                        [
                            'user_id' => $v,
                            'msg_id'  => $msgId
                        ]
                    );
                }
            }
        }
        return $msgId;
    }

    /**
     * 编辑消息
     */
    public function saveClcwMessage($id, $data = [])
    {
        if (empty($id) || empty($data)) {
            return false;
        }
        $receiveIds = $data['receive_ids'];
        unset($data['receive_ids']);
        if (!empty($receiveIds)) {
            $receiveIdsArr = explode(',', $receiveIds);
            $condition = [];
            $condition['msg_id'] = ['eq', $id];
            $condition['user_id'] = ['not in', $receiveIdsArr];
            $clcwMessageReceiveModel = D("ClcwMessageReceive", 'Model');
            $clcwMessageReceiveModel->where($condition)->delete();
            foreach ($receiveIdsArr as $k => $vb) {
                if (!empty($vb)) {
                    $isExist = $clcwMessageReceiveModel->where(
                        [
                            'msg_id'  => $id,
                            'user_id' => $vb
                        ]
                    )->count();
                    if (!$isExist) {
                        $clcwMessageReceiveModel->add(
                            [
                                'msg_id'  => $id,
                                'user_id' => $vb
                            ]
                        );
                    }
                }
            }
        }
        $data['update_time'] = NOW_TIME;
        return $this->clcwMessageModel->where(['id' => $id])->save($data);
    }
}